/*
 *  Copyright 2017 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.demo;

import com.google.blockly.ohos.AbstractBlocklyAbility;
import com.google.blockly.ohos.codegen.CodeGenerationRequest;
import com.google.blockly.ohos.codegen.LanguageDefinition;
import com.google.blockly.model.DefaultBlocks;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.Arrays;
import java.util.List;


/**
 * Demo ability that programmatically adds a view to split the screen between the Blockly workspace
 * and an arbitrary other view or Fraction and generates Lua code.
 */
public class LuaAbility extends AbstractBlocklyAbility {
    private static final String TAG = "LuaAbility";

    private static final String SAVE_FILENAME = "lua_workspace.xml";
    private static final String AUTOSAVE_FILENAME = "lua_workspace_temp.xml";
    // Add custom blocks to this list.
    private static final List<String> BLOCK_DEFINITIONS = DefaultBlocks.getAllBlockDefinitions();
    private static final List<String> LUA_GENERATORS = Arrays.asList();

    private static final LanguageDefinition LUA_LANGUAGE_DEF
            = LanguageDefinition.LUA_LANGUAGE_DEFINITION;

    private Text mGeneratedTextView;
    private EventHandler mHandler;

    private String mNoCodeText;

    CodeGenerationRequest.CodeGeneratorCallback mCodeGeneratorCallback =
            new CodeGenerationRequest.CodeGeneratorCallback() {
                @Override
                public void onFinishCodeGeneration(final String generatedCode) {
                    mHandler.postTask(new Runnable() {
                        @Override
                        public void run() {
                            mGeneratedTextView.setText(generatedCode);
                            DemoUtil.updateTextMinWidth(mGeneratedTextView, LuaAbility.this);
                        }
                    });
                }
            };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        mHandler = new EventHandler(EventRunner.getMainEventRunner());
        mGeneratedTextView = (Text) findComponentById(ResourceTable.Id_generated_code);
        DemoUtil.updateTextMinWidth(mGeneratedTextView, this);
        mNoCodeText = mGeneratedTextView.getText(); // Capture initial value.
    }

    @Override
    public int getBlocklyLayoutRes() {
        return ResourceTable.Layout_split_content;
    }

    @Override
    protected List<String> getBlockDefinitionsJsonPaths() {
        return BLOCK_DEFINITIONS;
    }

    @Override
    protected LanguageDefinition getBlockGeneratorLanguage() {
        return LUA_LANGUAGE_DEF;
    }

    @Override
    protected String getToolboxContentsXmlPath() {
        return DefaultBlocks.TOOLBOX_PATH;
    }

    @Override
    protected List<String> getGeneratorsJsPaths() {
        return LUA_GENERATORS;
    }

    @Override
    protected CodeGenerationRequest.CodeGeneratorCallback getCodeGenerationCallback() {
        // Uses the same callback for every generation call.
        return mCodeGeneratorCallback;
    }

    @Override
    public void onClearWorkspace() {
        super.onClearWorkspace();
        mGeneratedTextView.setText(mNoCodeText);
        DemoUtil.updateTextMinWidth(mGeneratedTextView, this);
    }

    /**
     * Optional override of the save path, since this demo Ability has multiple Blockly
     * configurations.
     *
     * @return Workspace save path used by this Ability.
     */
    @Override
    protected String getWorkspaceSavePath() {
        return SAVE_FILENAME;
    }

    /**
     * Optional override of the auto-save path, since this demo Ability has multiple Blockly
     * configurations.
     *
     * @return Workspace auto-save path used by this Ability.
     */
    @Override
    protected String getWorkspaceAutosavePath() {
        return AUTOSAVE_FILENAME;
    }
}
